using System.Drawing;
using System.IO;
using GTA;
using GTA.Native;
using NativeUI;

namespace CustomPeds.Menus;

public class GirlfriendMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.gf_Menu, new Point(0, -107));
    private static bool inited;
    public static string DefaultSkinGF = "Default";

    public GirlfriendMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            Build();
            inited = true;
        }
    }

    public static void Open()
    {
        if (!inited) return;
        Build();
        menu.Visible = true;
        menu.RefreshIndex();
    }

    public static void Build()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());

        var dressing = new UIMenuItem(TextSetting.pg_MenuItem_DressingRoom);
        var driveToWp = new UIMenuItem(TextSetting.pg_MenuItem_DriveToWaypoint);
        var clean = new UIMenuItem(TextSetting.pg_MenuItem_Clean);
        var settings = new UIMenuItem(TextSetting.pg_MenuItem_Settings);
        var preloaded = new UIMenuItem(TextSetting.gf_MenuItem_PreLoaded);
        var recreate = new UIMenuItem(TextSetting.gf_MenuItem_ReCreate);

        dressing.Activated += (m, i) =>
        {
            GTA.UI.Screen.FadeOut(1);
            DressingScript.InitDressing(Main.Member.P2);
            DressingRoomMenu.InitDressingRoom();
            menu.Visible = false;
            Main.OnMainMenuHidden();
            DressingRoomMenu.menu.Visible = true;
            GTA.UI.Screen.FadeIn(500);
        };
        driveToWp.Activated += (m, i) => { Main.P2.DetectDriverAndWaypoint(); Main.OnMainMenuHidden(); menu.Visible = false; };
        clean.Activated += (m, i) =>
        {
            Main.P2.Character.ClearBloodDamage();
            Function.Call((Hash)0x9C720776DAA43E7E, Main.P2.Character);
            Main.P2.Character.ClearVisibleDamage();
            Main.P2.Character.RemoveHelmet(instantly: true);
            if (Main.P2.Character.CurrentVehicle != null && Main.P2.Character.IsInVehicle(Main.P2.Character.CurrentVehicle))
            {
                Main.P2.Character.CurrentVehicle.Wash();
                Main.P2.Character.CurrentVehicle.Repair();
            }
            menu.Visible = false;
            Main.OnMainMenuHidden();
        };
        settings.Activated += (m, i) =>
        {
            // 打开设置菜单，确保可见
            SettingMenu.InitCharacterSettingMenu(Main.Member.P2);
        };
        preloaded.Activated += (m, i) =>
        {
            PreLoadedMenu.InitPreLoadedMenu();
            // 隐藏上级以避免界面重叠
            menu.Visible = false;
        };
        recreate.Activated += (m, i) =>
        {
            if (Main.P2.Character != null)
            {
                Main.P2.Character.MarkAsNoLongerNeeded();
                Main.P2.Character.Delete();
            }
            var nearbyPeds = World.GetNearbyPeds(Game.Player.Character.Position, 100f);
            foreach (var ped in nearbyPeds)
            {
                if ((ped.IsInGroup || ped.IsDead) && ped.Handle != Game.Player.Character.Handle)
                {
                    ped.MarkAsNoLongerNeeded();
                    ped.Delete();
                }
            }
            Main.P2.Character = null;
            Main.InitFemale();
            // 重新创建后，确保基本状态安全，避免随后打开互动菜单时空引用
            try
            {
                if (Main.P2.Character != null)
                {
                    Main.P2.Character.Task.ClearAllImmediately();
                    Main.P2.Character.BlockPermanentEvents = true;
                    Main.P2.Character.IsPersistent = true;
                    Main.P2.Character.CanWearHelmet = Main.P2.CanWearHelmet;
                    Main.P2.Character.IsInvincible = Main.P2.Invincible;
                }
            }
            catch { }
            if (File.Exists($"{Skin.SKIN_PATH}\\{Gender.Female}\\{DefaultSkinGF}.skin"))
            {
                Skin.SkinFileLoad(ref Main.P2, $"{Skin.SKIN_PATH}\\{Gender.Female}\\{DefaultSkinGF}.skin");
            }
            if (File.Exists($"{Skin.SKIN_PATH}\\{Gender.Male}\\{DefaultSkinGF}.skin"))
            {
                Skin.SkinFileLoad(ref Main.P2, $"{Skin.SKIN_PATH}\\{Gender.Male}\\{DefaultSkinGF}.skin");
            }
            Main.P2.skin.SetWeapons(Main.P1.skin.GetWeaponsData());
            // 不自动关闭当前菜单，交由用户决定返回，避免紧接着打开主菜单时的竞态
        };

        menu.AddItem(dressing);
        menu.AddItem(driveToWp);
        menu.AddItem(clean);
        menu.AddItem(settings);
        menu.AddItem(preloaded);
        menu.AddItem(recreate);

        // 根据 P2 状态与载具情况启用/禁用，与 Reference 对齐
        if (Main.P2.Character == null || Main.P2.Character.IsDead)
        {
            for (int i = 0; i < menu.MenuItems.Count; i++)
            {
                menu.MenuItems[i].Enabled = false;
            }
            menu.MenuItems[4].Enabled = true; // PreLoaded
            menu.MenuItems[5].Enabled = true; // ReCreate
            return;
        }
        for (int j = 0; j < menu.MenuItems.Count; j++)
        {
            menu.MenuItems[j].Enabled = true;
        }
        if (Game.Player.Character.IsInVehicle())
        {
            menu.MenuItems[0].Enabled = false; // DressingRoom
            menu.MenuItems[1].Enabled = true;  // DriveToWaypoint
        }
        else
        {
            menu.MenuItems[0].Enabled = true;
            menu.MenuItems[1].Enabled = false;
        }
    }
}


